﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SVAP
{
    public partial class AddAlgorithmForm : Form
    {
        private static string strAlgoDescription = "";// Algorithm description
        private static string strFileName = "";// Algorithm Name
        private static string strFromPath = @"C:\";// source path for algorithms
        private static string strToPath = "";// destination path to copy file
        //FormLoad()时从文件中读取，每次添加都对其更新
        private static List<string> listAlgorithm = new List<string>();// store the file name of the DLL file to avoid repetition
        private static bool addToConfig = true;

        public AddAlgorithmForm()
        {
            InitializeComponent();
        }

        private void buttonAlgoBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = @"C:\";
            //file filter
            string filter = "DLL库文件(*.dll)|*.dll";
            openFileDialog1.Filter = filter;
            openFileDialog1.Multiselect = false;

            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            else
            {
                this.Text = openFileDialog1.FileName;//change the form text to the file name.

                strFromPath = openFileDialog1.FileName;// set source path
                strToPath = System.AppDomain.CurrentDomain.BaseDirectory;// set destination path
                strFileName = System.IO.Path.GetFileName(strFromPath);// file name, eg. "test.dll"
                strToPath += @"Algorithm\";
                strToPath += strFileName;
                textBoxAlgoBrowse.Text = strFromPath;
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (strFromPath != "" && strToPath != "")
            {
                if (textBoxAlgoDescription.Text != "")
                {
                    // get description of the algorithm
                    strAlgoDescription = textBoxAlgoDescription.Text;

                    // update file info in Config.txt
                    string strConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory;// set destination path
                    strConfigFilePath += @"Config.txt";

                    FileStream fs = new FileStream(strConfigFilePath, FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs,Encoding.Default);

                    //strFileName = System.IO.Path.GetFileName(strFromPath);// file name, eg. "test.dll"
                    string strContent = "";
                    //strContent = strFileName + " " + strAlgoDescription + " " + "9\n";
                    strContent = strFileName + " " + strAlgoDescription + "\n";
                    //写入配置文件的和写入显示列表的不一样
                    //string strContent1 = "文件名：" + strFileName + " " + "算法描述：" + strAlgoDescription + " 使用频率" + "9";
                    string strContent1 = "文件名：" + strFileName + " " + "算法描述：" + strAlgoDescription ;
                    //MessageBox.Show(strConfigFilePath);

                    foreach (string str in listAlgorithm)
                    {
                        if (str == strContent)
                        {
                            addToConfig = false;
                        }
                    }
                    // avoid repetition
                    if (addToConfig)
                    {
                        // copy file
                        File.Delete(strToPath);
                        File.Copy(strFromPath, strToPath);
                        //把添加的配置文件信息 “文件名 算法描述” 写到配置文件中
                        sw.WriteLine(strContent);
                        listAlgorithm.Add(strContent);
                        addToConfig = true;
                    }
                    else
                    {
                        MessageBox.Show("该文件已经存在，请勿重复添加！");
                    }

                    sw.Flush();
                    sw.Close();
                    fs.Close();

                    ManageAlgorithmForm maf = new ManageAlgorithmForm();
                    maf = (ManageAlgorithmForm)this.Owner;
                    maf.listBox_showAlgorithm.Items.Add(strContent1);
                }
                else
                {
                    MessageBox.Show("算法描述不能为空！");
                }

            }

            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddAlgorithmForm_Load(object sender, EventArgs e)
        {
            string strFromPath = System.AppDomain.CurrentDomain.BaseDirectory;
            strFromPath += @"\Config.txt";
            StreamReader sr = new StreamReader(strFromPath,Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null)
                    listAlgorithm.Add(sLine);
            }
            sr.Close();
        }
    }
}
